﻿using Abp;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using FwProject.Product.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.BrandInfo;
using FwProject.ShoppingMall.Product;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FwProject.Product
{
    public class ProductTypeBrandAppService : FwProjectAppServiceBase, IProductTypeBrandAppService
    {

        private readonly IRepository<ProductType_Brand, Guid> _repositoryProductTypeBrand;
        private readonly IRepository<Brand, Guid> _repositoryBrand;
        private IGuidGenerator _guid { get; set; }
        private IObjectMapper _map { get; set; }

        public ProductTypeBrandAppService(IRepository<ProductType_Brand, Guid> repositoryProductTypeBrand, IRepository<Brand, Guid> repositoryBrand)
        {
            _repositoryProductTypeBrand = repositoryProductTypeBrand;
            _repositoryBrand = repositoryBrand;
            _guid = SequentialGuidGenerator.Instance;
        }

        public async Task UpdatedBatchAsync(Guid id, ProductTypeBrandEnterDto input)
        {
            var original = _repositoryProductTypeBrand.GetAll().Where(o => o.ProductTypeid == id).ToList();
            if (!string.IsNullOrWhiteSpace(input.Ids))
            {
                Guid temp;
                //获取提交的新参与人Id集合
                var brandids = input.Ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(o => { Guid.TryParse(o, out temp); return temp; });
                if (brandids.Any())//判断是否有元素
                {
                    if (original.Count > 0)
                    {
                        //获取新参与人与原参与人之间的差异数据
                        var removes = original.Where(o => brandids.Any(r => r == o.ProductTypebrandid) == false).ToList();
                        if (removes?.Count > 0)
                        {
                            for (int i = 0; i < removes.Count; i++)//执行删除操作
                                await _repositoryProductTypeBrand.DeleteAsync(removes[i]);
                        }
                    }
                    //获取添加的元素
                    var adds = brandids.Where(r => original.Any(o => o.ProductTypebrandid == r) == false);
                    foreach (var a in adds)
                        await _repositoryProductTypeBrand.InsertAndGetIdAsync(new ProductType_Brand { Id = _guid.Create(), ProductTypebrandid = a, ProductTypeid = id });
                }
            }
            else
            {
                if (original.Count > 0)
                {
                    for (int i = 0; i < original.Count; i++)//执行删除操作
                        await _repositoryProductTypeBrand.DeleteAsync(original[i]);
                }
            }

        }

        public List<ProductTypeBrandOutput> GetOutputDto(Guid id)
        {
            var brands = _repositoryBrand.GetAll();
            var query = _repositoryProductTypeBrand.GetAll().Where(o => o.ProductTypeid == id);

           var list= query.Join(brands, q => q.ProductTypebrandid, b => b.Id, (q, b) => new ProductTypeBrandOutput()
            {
                Id = q.Id,
                BrandId = b.Id,
                BrandName = b.BrandName
            }).ToList();

            return list;
        }
    }
}
